﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PoshBoard.VisualDesigner.Controls
{
    public class NewTreeView : TreeView, IPBComponentBase
    {
        public string CommandBase { get { return "New-TreeView"; } }

        public string PSName { get; set; }

        public void BuildPSName(string parentName)
        {
            this.PSName = "$" + parentName + "_" + this.Name;
        }

        public string GetParameters()
        {
            string param = PSScriptHelper.GetCommonParameters(this);

            return param;
        }

        public string GetConstructionScript()
        {
            string script =  PSScriptHelper.BuildPSScript(this, this.Name, null);

            string childrenScript = buildChildrenScript(this.PSName, this.Items);

            return script + childrenScript;
        }


        private string buildChildrenScript(string parentPSName, ItemCollection parentItems)
        {
            string childrenScript = string.Empty;
            int i = 0;
            foreach (TreeViewItem item in parentItems)
            {
                string itemPSName = parentPSName + "_" + "TreeViewItem" + i.ToString();
                childrenScript += Environment.NewLine;
                childrenScript += itemPSName + "=New-TreeViewItem -Text " + Common.QuoteChar + item.Header + Common.QuoteChar;
                childrenScript += Environment.NewLine;
                childrenScript += "Add-PBChild -inputObject " + parentPSName + " -Children " + itemPSName;

                childrenScript += buildChildrenScript(itemPSName, item.Items);
                
                i++;
            }
            return childrenScript;
        }

    }
}
